
/***  DOCUMENTATION LAYER

	rc4 encryption library

	Last Update: Mar 2007
	Author: http://sourceforge.net/users/jhanna777/ (amended slightly by Tom at klenwell@gmail.com)
	License: GPL License (http://www.opensource.org/licenses/gpl-license.php)

FUNCTIONS	
	rc4_encrypt(str_key, str_plaintext)
	rc4_decrypt(str_key, str_ciphertext)
	rc4(key, text)
	textToBase64(t)
	base64ToText(t)
	makeArray(n)

NOTES
	base-64 character set (var b64s) adjusted to coordinate with md5 function

	To link: <script type="text/javascript" src="path/to/file.js"></script>

REFERENCE
	rc4 script source: http://shop-js.sourceforge.net/crypto.htm
	license details: http://sourceforge.net/projects/shop-js/
	http://developer.mozilla.org/en/docs/JavaScript

______________________________________________________________________________*/

// global values
var b64s='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$+';


// rc4_encrypt
/*____________________________________________________________________________*/
function rc4_encrypt(str_key, str_plaintext)
{
	return textToBase64(rc4(str_key,str_plaintext));	
}
/*____________________________________________________________________________*/


// rc4_decrypt
/*____________________________________________________________________________*/
function rc4_decrypt(str_key, str_ciphertext)
{
	return rc4(str_key,base64ToText(str_ciphertext));	
}
/*____________________________________________________________________________*/


// rc4
/*____________________________________________________________________________*/
function rc4(key, text) {
 var i, x, y, t, x2;
 s=makeArray(0);

 for (i=0; i<256; i++) {
  s[i]=i
 }
 y=0
 for (x=0; x<256; x++) {
  y=(key.charCodeAt(x % key.length) + s[x] + y) % 256
  t=s[x]; s[x]=s[y]; s[y]=t
 }
 x=0;  y=0;
 var z=""
 for (x=0; x<text.length; x++) {
  x2=x % 256
  y=( s[x2] + y) % 256
  t=s[x2]; s[x2]=s[y]; s[y]=t
  z+= String.fromCharCode((text.charCodeAt(x) ^ s[(s[x2] + s[y]) % 256]))
 }
 return z
} // end Fx
/*____________________________________________________________________________*/


// textToBase64
/*____________________________________________________________________________*/
function textToBase64(t) {
 var r=''; var m=0; var a=0; var tl=t.length-1; var c
 for(n=0; n<=tl; n++) {
  c=t.charCodeAt(n)
  r+=b64s.charAt((c << m | a) & 63)
  a = c >> (6-m)
  m+=2
  if(m==6 || n==tl) {
   r+=b64s.charAt(a)
	 
	 // disable -- break js_to_php translations (viz., unserialize)
   //if((n%45)==44) {r+="\n"}
	 
   m=0
   a=0
  }
 }
 return r
} // end Fx
/*____________________________________________________________________________*/


// base64ToText
/*____________________________________________________________________________*/
function base64ToText(t) {
 var r=''; var m=0; var a=0; var c
 for(n=0; n<t.length; n++) {
  c=b64s.indexOf(t.charAt(n))
  if(c >= 0) {
   if(m) {
    r+=String.fromCharCode((c << (8-m))&255 | a)
   }
   a = c >> m
   m+=2
   if(m==8) { m=0 }
  }
 }
 return r
} // end Fx
/*____________________________________________________________________________*/


// makeArray
/*____________________________________________________________________________*/
function makeArray(n) {
 for (var i=1; i<=n; i++) {
  this[i]=0
 }
 return this
}
/*____________________________________________________________________________*/


